$(function(){
	var mapDa = new Map();
	var mapSelect = new Map();
	var selIconCount = 0;
	//图标1-n 的组数
	//var iconGroupCounts = [3,5,4,2,4];
	var iconGroupCounts = [3,5,4,5,4,5,2,5,4,5,3,3];
	//每层 图标个数(注意：总和需要等于 iconGroupCounts总和的3倍)
	//var layerIconCounts=[4,8,6,9,12,7,8];
	var layerIconCounts=[1,4,6,18,10,8,15,12,16,13,12,5,14,10];
	//每一层图标 起始点偏移位置，以及矩阵大小
	//var layerStartXY = [[24,3],[12,5],[12,5],[12,5],[12,5],[12,5],[12,5]];
	var layerStartXY = [[42,1],[28,3],[28,3],[14,5],[14,5],[14,5],[14,5],[14,5],[14,5],[14,5],[14,5],[28,3],[14,5],[14,5]];
	//生成图标数组
	var iconRandomArr = genIconList(iconGroupCounts);
	//随机打乱顺序
	randomSort(iconRandomArr);
	
	var gradx=7,grady=7;
	
	//生成图标地图
	//每层icon 的数量
	var htm = '';
	var idnum = 0;
	var iconwidth=14;
	
	//循环每层
	for(var layerIndex=0;layerIndex<layerIconCounts.length;layerIndex++){
		//每层 放标签个数
		for(var c=1;c<=layerIconCounts[layerIndex];c++){
			//在layerStartXY[layerIndex] 矩阵范围内随机生成图标
			htm =genIconHtml(iconRandomArr[idnum],layerIndex,layerStartXY[layerIndex]);
			idnum++;
			$(".icon-box").append(htm);
		}
	}
		
	checkHideOrShow();
		
	$(".item").click(function(){
		var music = document.getElementById("music1");
		if(music.paused){
			music.paused=false;
			//music.play();
		}
		if($(this).hasClass('item-hide')){
			return;
		}
		if(selIconCount==7){
			alert("游戏已经结束了。。。。你输了！");
			return;
		}
		selectIconAction($(this));
	})
	
	$(".btn").click(function(){
		daojishi();
	});
	
function daojishi(){
	var num =10;
	$(".gg").show();
	$(".body-box").hide();
	
	var myVar = setInterval(function(){ 
		num--;
		console.log(num);
		$(".gg").text("（基尼泰美）倒计时："+num);
		if(num==0){
			clearInterval(myVar);
		}
	}, 1000);
	setTimeout(function(){
		$(".gg").hide();
		$(".body-box").show();
		refresh();
	},10000);
	
}

function selectIconAction(selectObj){
	var lefy = selIconCount*15;
	$(selectObj).animate({top:'115%',left:lefy+'%'},function(){
		var selectIcon = $(selectObj).remove();
		checkHideOrShow();
		var iconCount = $(".sel-box").find(".item").length;
		$(selectObj).css('left','');
		$(selectObj).css('top','0px');
		$(".sel-box").append(selectIcon);
		selIconCount++;
		
		var imgIdx = $(selectObj).attr("imgIdx");
		if(mapSelect.has(imgIdx)){
			mapSelect.set(imgIdx,mapSelect.get(imgIdx)+1);
			if(mapSelect.get(imgIdx)==3){
				removeIconByImgId(imgIdx);
				mapSelect.set(imgIdx,0);
			}
		}else{
			mapSelect.set(imgIdx,1);
		}
		checkResult();
	});
	
}

function checkResult(){
	if(selIconCount==7){
		$(".gameover").show();
		return;
	}
	var len = $(".icon-box").find(".item").length;
	if(len==0){
		$(".gameover").text('游戏胜利').show();
	}
}

function removeIconByImgId(imgIdx){
	$(".sel-box").find(".item").each(function(item){
		var iconImgIdx = $(this).attr("imgIdx");
		if(imgIdx==iconImgIdx){
			$(this).remove();
			selIconCount--;
		}
	});
}

//在layerStartXY n*n矩阵方位内生成图标
function genIconHtml(imgIdx,layerIndex,layerStartXY){
	var active =true;
	var randomX =  0;
	var randomY = 0;
	var rand = 0;
	while(active){
		active=false;
		rand = Math.floor(Math.random()*layerStartXY[1]*layerStartXY[1]);
		randomX = rand%layerStartXY[1];
		randomY = Math.floor(rand/layerStartXY[1]);
		
		var vl = layerIndex*100+rand;
		if(mapDa.has(vl)){
			active = true;
		}else{
			mapDa.set(vl,1);
		}
	}
	var pri = 0;
	var lt = Math.floor(Math.random()*4);
	if(layerIndex%2==0){
		pri = 7;
	}
	var x =0;
	var y=0;
	if(lt==0){
		x = randomX*iconwidth+pri+layerStartXY[0];
		y = randomY*iconwidth+layerStartXY[0];
	}else if(lt==1){
		x = randomX*iconwidth-pri+layerStartXY[0];
		y = randomY*iconwidth+layerStartXY[0];
	}else if(lt==2){
		x = randomX*iconwidth+layerStartXY[0];
		y = randomY*iconwidth-pri+layerStartXY[0];
	}else if(lt==3){
		x = randomX*iconwidth+layerStartXY[0];
		y = randomY*iconwidth+pri+layerStartXY[0];
	}
	
	var imgsrc = 'img/'+imgIdx+'.jpeg';
	var htm = '<div imgIdx='+imgIdx+' x='+x+' y='+y+' dep='+layerIndex+' class="item" style="left:'+x+'%;top:'+y+'%;background-image:url('+imgsrc+')"></div>';
	return htm;
}

function genIconList(iconGroupCounts){
	var iconRandomArr = [];
	for(var iconIndx=1;iconIndx<=iconGroupCounts.length;iconIndx++){
		for(var idx=0;idx<3*iconGroupCounts[iconIndx-1];idx++){
			iconRandomArr.push(iconIndx);
		}
	}
	return iconRandomArr;
}

//随机打乱数组排序
function randomSort(iconRandomArr){
	for(var i=0;i<iconRandomArr.length;i++){
		var randomNum = Math.floor(Math.random()*iconRandomArr.length);
		var temp = iconRandomArr[i];
		iconRandomArr[i]=iconRandomArr[randomNum];
		iconRandomArr[randomNum] = temp;
	}
	return iconRandomArr;
}

//设置图标可选和不可选
function checkHideOrShow(){
	$(".icon-box").find(".item").each(function(item){
		var x = parseInt($(this).attr("x"));
		var y = parseInt($(this).attr("y"));
		var dep = parseInt($(this).attr("dep"));
		var thisIcon = $(this);
		var show = true;
		$(this).nextAll().each(function(xitem){
			var x2 = parseInt($(this).attr("x"));
			var y2 = parseInt($(this).attr("y"));
			var dep2 = parseInt($(this).attr("dep"));
			if( x2>x-iconwidth && y2>y-iconwidth && x2<x+iconwidth && y2<y+iconwidth){
				$(thisIcon).addClass('item-hide');
				show = false;
				return false;
			}
		});
		if(show){
			$(thisIcon).removeClass('item-hide');
		}
		
	});
}

function refresh(){
	var len = $(".icon-box").find(".item").length;
	$(".icon-box").find(".item").each(function(item){
		var imgIdx = $(this).attr("imgIdx");
		var imgsrc = 'url(img/'+imgIdx+'.jpeg)';
		var rand = Math.floor(Math.random()*len);
		var i=0;
		var this1 = $(this);
		$(".icon-box").find(".item").each(function(item3){
			if(rand==i){
				var imgIdx2 = $(this).attr("imgIdx");
				var imgsrc2 = 'url(img/'+imgIdx2+'.jpeg)';
				$(this1).attr("imgIdx",imgIdx2);
				$(this1).css('background-image',imgsrc2);
				
				$(this).attr("imgIdx",imgIdx);
				$(this).css('background-image',imgsrc);
			}
			i++;
		});
	})
}
})